package com.matt.repository.base;

import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import java.util.List;
import java.util.Objects;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/11
 * *******************************
 */
@Service
public class BaseNativeSqlRepository {

    @PersistenceUnit
    private EntityManagerFactory emf;

    /**
     * 查询多个属性
     * 返回List<Object[]>数组形式的List，数组中内容按照查询字段先后
     * @param sql   原生SQL语句
     * @return
     */
    public List<Object[]> sqlArrayList(String sql){
        EntityManager em=emf.createEntityManager();
        Query query=em.createNativeQuery(sql);
        List<Object[]> list = query.getResultList();
        em.close();
        return  list;
    }


    /**
     * 查询多个属性
     * 返回List<Object>对象形式的List，Object为Class格式对象
     * @param sql   原生SQL语句
     * @param obj   Class格式对象
     * @return
     */
    public List sqlObjectList(String sql, Object obj){
        EntityManager em=emf.createEntityManager();
        Query query=em.createNativeQuery(sql, obj.getClass());
        List list = query.getResultList();
        em.close();
        return  list;
    }

    /**
     * 查询单个属性
     * 返回List<Object>对象形式的List，Object为对象数据类型
     * @param sql  原生SQL语句
     * @return
     */
    public List sqlSingleList(String sql){
        EntityManager em=emf.createEntityManager();
        Query query=em.createNativeQuery(sql);
        List list = query.getResultList();
        em.close();
        return  list;
    }

    public Integer sqlResult(String sql){
        EntityManager em=emf.createEntityManager();
        Query query=em.createNativeQuery(sql);
        Integer result = query.getFirstResult();
        em.close();
        return result;
    }

    public Long sqlLongResult(String sqlTemplate) {
        EntityManager em = emf.createEntityManager();
        Query query = em.createNativeQuery(sqlTemplate);

        Object o = query.getSingleResult();
        em.close();
        return Objects.isNull(o) ?0L:Long.valueOf(o.toString());
    }

    public Long sqlLongResult(String sqlTemplate, List params) {
        EntityManager em = emf.createEntityManager();
        Query query = em.createNativeQuery(sqlTemplate);

        for(int i = 0 ; i < params.size(); i++) {
            query.setParameter("" + (i+1), params.get(i));
        }

        Object o = query.getSingleResult();
        em.close();
        return Objects.isNull(o) ?0L:Long.valueOf(o.toString());
    }

    public List sqlObjectList(String sql, Class clazz){
        EntityManager em=emf.createEntityManager();
        Query query=em.createNativeQuery(sql);
        query.unwrap(SQLQuery.class)
                .setResultTransformer(Transformers.aliasToBean(clazz))
                .list();
        List list = query.getResultList();
        em.close();
        return  list;
    }


}
